package book;

import java.util.ArrayList;
import java.util.List;

public class BookDaoImpl implements BookDao{

	private List<Book> bookList = new ArrayList<>();
	
	@Override
	public boolean insert(Book book) {
		if (bookList.contains(book)) {
			return false;
		}else {
			return bookList.add(book);
		}
	}

	@Override
	public boolean deleteById(int id) {
		// 1.封装对象
		Book book = new Book(id);
		// 2.移除
		return bookList.remove(book);
	}

	@Override
	public boolean update(Book book) {
		//1. 判断是否包含
		int index = bookList.indexOf(book);
		if (index != -1) {
			bookList.set(index, book);
			return true;
		}else {
			return false;
		}
	}

	@Override
	public Book selectById(int id) {
		// 1.封装Book
		Book b = new Book(id);
		// 2.查找下标
		int index = bookList.indexOf(b);
		// 3.没找到返回-1， 找到返回集合中的此书
		return index == -1 ? null : bookList.get(index);
	}

	@Override
	public List<Book> selectAll() {
		return bookList;
	}
}
